<template>
  <div>
    <a-form-model>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="申请表" required>
          <SearchTable @f_setTable="s_setTable" field="apltable" :value="currentObj.apltable" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="企业表">
          <SearchTable @f_setTable="s_setTable" field="enttable" :value="currentObj.enttable" />
        </a-form-model-item> </a-col
      ><a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="是否必填" required>
          <RadioYesNo :value="currentObj.require" field="require" @f_setYesNo="s_setYesNo" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="节点标题" required>
          <a-input style="width: 70%" v-model="currentObj.label" placeholder="请输入节点标题" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="是否顶级" required>
          <RadioYesNo :value="currentObj.toplevel" field="toplevel" @f_setYesNo="s_setYesNo" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="添加本级" required>
          <RadioYesNo :value="currentObj.isaddown" field="isaddown" @f_setYesNo="s_setYesNo" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="同步补正" required>
          <RadioYesNo :value="currentObj.isappend" field="isappend" @f_setYesNo="s_setYesNo" />
        </a-form-model-item>
      </a-col>

      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="行项数" required>
          <a-input-number style="width: 70%" v-model="currentObj.span" placeholder="请输入节点标题" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="申请表关联字段" required>
          <SearchField
            style="width: 70%"
            @f_setField="s_setField"
            :f_treeData="currentObj.aplfieldlist"
            :value="currentObj.relationfield"
            field="relationfield"
          />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="企业表关联字段" required>
          <SearchField
            style="width: 70%"
            @f_setField="s_setField"
            :f_treeData="currentObj.entfieldlist"
            :value="currentObj.entrelationfield"
            field="entrelationfield"
          />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="显示字段" required>
          <SearchField
            style="width: 70%"
            @f_setField="s_setField"
            :f_treeData="currentObj.aplfieldlist"
            :value="currentObj.primary"
            field="primary"
          />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="从表配置">
          <SearchTable @f_setTable="s_setSubTable" field="subtable" />
          <div v-for="item in currentObj.sublist" :key="item.apltable">
            {{ `${item.apltable} - ${item.label}` }} <a @click="handEdit(item)" href="javascript:void(0);">编辑</a>
            <a-popconfirm title="确定删除吗？" ok-text="是" cancel-text="否" @confirm="handDel(slotIndex)">
              <a href="javascript:void(0);" style="color: red; margin-left: 8px">删除</a>
            </a-popconfirm>
          </div>
        </a-form-model-item>
      </a-col>
    </a-form-model>

    <!-- 从表配置模态框 -->
    <SubModal ref="subModalRef" />
  </div>
</template>
<script>
import { currentObj, setSubCurrentObj } from '@/utils/formassembly/generaltree.js'
import { findTable } from '@/api/base/table.js'
import { DATA_BASE_SFAPPROVE } from '@/store/mutation-types.js'
export default {
  data() {
    return {
      currentObj: currentObj,
      labelCol: { span: 4 }, // label宽
      wrapperCol: { span: 20 }, // 组件宽
    }
  },
  created() {},
  methods: {
    // 从表删除
    handDel(index) {
      this.currentObj.sublist.splice(index, 1)
    },
    // 从表编辑
    handEdit(item) {
      // 把编辑值回显进去
      setSubCurrentObj(item)

      this.$refs.subModalRef.f_init(item.apltable)
    },
    // 从表配置
    s_setSubTable(value, field) {
      console.log('从表配置', value, field)
      this.$refs.subModalRef.f_init(value)
    },
    // 从表关联字段回调
    s_setField(value, field) {
      this.currentObj[field] = value || null
    },
    // 单选组件值
    s_setYesNo(value, field) {
      // console.log('单选', value, field, f_field)
      this.currentObj[field] = value
    },
    // 回显选中的表
    async s_setTable(value, field, index) {
      this.currentObj[field] = value
      // 如果点了清除按钮, 不做处理
      if (!value) {
        return
      }
      let fieldlist = []
      let res = await findTable({ database: DATA_BASE_SFAPPROVE, tableName: value })
      if (res.code == 0) {
        res.data.data.forEach((v) => {
            fieldlist.push({
              title: `${v.fieldName} - ${v.fieldDescription}`,
              value: v.fieldName,
            })
          })
      }
      // 根据选择的表类型加字段
      if (field == 'apltable') {
        this.currentObj.aplfieldlist = fieldlist
        this.currentObj.fieldlist = []
        fieldlist.forEach((v) => {
          this.currentObj.fieldlist.push({
            t: v.value,
            b: undefined,
            hidden: false,
            disable: false,
            option: {
              type: undefined,
            },
            comments: v.title.substring(v.title.indexOf(' - ') + 3, v.title.length),
          })
        })
      } else if (field == 'enttable') {
        this.currentObj.entfieldlist = fieldlist
        if (this.currentObj.fieldlist && this.currentObj.fieldlist.length > 0) {
          let entMap = {}
          // 企业表字段转MAP
          fieldlist.forEach((v) => {
            entMap[v.value] = v.value
          })
          // 根据之前的申请表字段进行匹配, 同名的字段赋值
          this.currentObj.fieldlist.forEach((v) => {
            // console.log('---', v, entMap)
            if (v.t in entMap) {
              v.b = entMap[v.t]
            }
          })
        }
      }
      console.log('currentObj', this.currentObj, this.currentObj.fieldlist)
    },
  },
  components: {
    SearchTable: () => import('@/views/businessAssembly/public/spareparts/searchTable'),
    RadioYesNo: () => import('@/views/businessAssembly/public/spareparts/radioYesNo'),
    SearchField: () => import('@/views/businessAssembly/public/spareparts/searchField'),
    SubModal: () => import('@/views/businessAssembly/generaltree/subtable/subModal.vue'),
  },
}
</script>

<style lang="scss" scoped>
</style>
